import java.io.*;
import java.util.*;
public class exec_rmfiles {

	public static void main(String[] args) throws Exception{
		System.out.println("usage: java exec_rmfiles <option- 1/2> [<skiplines> <sleep1> <sleep2> <minlengthinkb> <bytearraylength> <pattern>]");
		String filename="rm1_files.sh";
		if(args.length<1){
			System.out.println("specify '1' or '2' : \"one way file -1 (rm1_files.sh -default) or 2 way file -2(rm2_files.sh) \"");
			System.exit(0);
		}else {
			if(args[0].equals("2"))
				filename="rm2_files.sh";
			
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
		String line="";
		int skip_lines=0;
		int sleep=10;
		int sleep2=0;
		int minlength_in_kb=100;
		int barrlen=100000;
		String pattern="";
		if(args.length >1)
			skip_lines=Integer.parseInt(args[1]);
		if(args.length >2)
			sleep=Integer.parseInt(args[2]);
		if(args.length >3)
			sleep2=Integer.parseInt(args[3]);
		if(args.length >4)
			minlength_in_kb=Integer.parseInt(args[4]);
		if(args.length >5)
			barrlen=Integer.parseInt(args[5]);
		if(args.length >6)
			pattern=args[6];
		System.out.println("sleep="+sleep);
		int _cnt=0;
		while((line=br.readLine())!= null){
			_cnt++;
			if(_cnt<skip_lines){
				continue;
			}
			if(!line.toLowerCase().endsWith(pattern.toLowerCase()+"\""))
				continue;
			if(!line.startsWith("diff \""))
				continue;
			System.out.println("(("+_cnt+"))");
			line=line.substring("diff \"".length());
			int len=line.length();
			int i=line.indexOf("\" \"");
			int j=line.indexOf("\"",i+3);
			if(i<5 || j< 9 || (j!=(len-1))){ //should not happen
				System.out.println("\t\terr line>>\""+line+"\"");
				System.out.println("i="+i);
				System.out.println("j="+j);
				System.out.println("len="+len);
				Thread.sleep(1000000);
				continue;
			}
			String args0=line.substring(0,i);
			String args1=line.substring(i+3,j);
			File f0=new File(args0);
			File f1=new File(args1);
			if(! f0.exists()){
				System.out.println("\n\nnot existing file1==\""+args0+"\"\n\n");
				continue;
			}
			if(! f1.exists()){
				System.out.println("\n\nnot existing file2==\""+args1+"\"\n\n");
				continue;
			}
			if(f0.isFile() && f1.isFile()){
				if(f0.length() != f1.length()){
					System.out.println("\t\terr length mismatch==\""+args0+"\":\""+args1+"\"");
					Thread.sleep(10000);
					continue;
				}else{
					System.out.println("\tdiff \""+args0+"\" \""+args1+"\"\n");
					System.out.println("ls -l \""+args0+"\"");
					System.out.println("ls -l \""+args1+"\"\n\n");
					System.out.println("filename=1. \""+args0.substring(args0.lastIndexOf("/")+1)+"\"");
					System.out.println("filename=2. \""+args1.substring(args1.lastIndexOf("/")+1)+"\"\n\n-----------------");
					if(f0.length()<minlength_in_kb*1000){
						System.out.println("less than cutoff length "+minlength_in_kb+":"+f0.length());
						continue;
					}
					if(args0.trim().equals(args1.trim()) || args0.equals(args1)|| args0.trim().equals(args1) || args0.equals(args1.trim())){
						System.out.println("ERRRRRROOOOOORRRRRRRRRRRRRRRRRRRRRRRRRRRRRR");
						continue;
					}
					Date dt1 = new Date();
					if(dif.comp(new String[]{args0,args1})){
						Date dt2 = new Date();
						if(f0.length()>0)
							System.out.println("filename=-----------"+f0.length()+';'+((dt2.getTime()-dt1.getTime())*1000000L)/f0.length());
						System.out.println("diff match(removing seond):");
						System.out.println("\t1.\""+args0+"\"");
						System.out.println("\t2.\""+args1+"\"\n");
						Thread.sleep(1000*sleep);
						Process pc=Runtime.getRuntime().exec(new String[]{"rm","-f",args1});
						int iii=pc.waitFor();
						System.out.println("\nremoved seond:"+args1+"="+iii);
						System.out.println("ls  -l \""+args0+"\"");
						pc=Runtime.getRuntime().exec(new String[]{"ls","-l",args0});
						iii=pc.waitFor();
						BufferedReader br__ = new BufferedReader(new InputStreamReader(pc.getInputStream()));
						System.out.println(br__.readLine());
						br__.close();
						System.out.println("ls  -l \""+args1+"\"");
						pc=Runtime.getRuntime().exec(new String[]{"ls","-l",args1});
						iii=pc.waitFor();
						br__ = new BufferedReader(new InputStreamReader(pc.getInputStream()));
						System.out.println(br__.readLine());
						br__.close();
						System.out.println("-----------\n\n\n");
						Thread.sleep(sleep2*1000);
					}else{
						System.out.println("\tdiff MISMATCH\n");
						
					}
				}
			}else{
				System.out.println("\t\terr not files ==\""+args0+"\":\""+args1+"\"");
				Thread.sleep(10000);
				continue;
			}
			
			
		}
	}

}
